package attack;

import gameClass.MapChunk;
import gameTypes.Vector2;
import globals.Global;

public class PlaceTiles extends Attack {
	public String name = "Place Tile";
	public int[][] tileIndex;
	
	public PlaceTiles(int[][] tileIndex) {
		this.tileIndex = tileIndex;
	}
	
	@Override
	public void Action() {
		int tilex = (Global.Instance().input.mouseX + (int)Global.Instance().camera.getLocation().x)/Global.Instance().tileSize;
		int tiley = (Global.Instance().input.mouseY + (int)Global.Instance().camera.getLocation().y)/Global.Instance().tileSize;
		
		int chunkx = tilex / Global.Instance().chunkSize;
		int chunky = tiley / Global.Instance().chunkSize;
		
		tilex %= Global.Instance().chunkSize;
		tiley %= Global.Instance().chunkSize;
		
		int tileOffsetx = tilex;
		int chunkOffsetx = chunkx;
		try {
			for (int i = 0; i < tileIndex.length; i++) {
				
				int chunkOffsety = chunky;
				int tileOffsety = tiley;
				
				if (tileOffsetx >= Global.Instance().chunkSize) {
					tileOffsetx %= Global.Instance().chunkSize;
					chunkOffsetx++;
				}
				
				try {
					for (int j = 0; j < tileIndex[i].length; j++) {
						// Get correct chunk for offset
						
						if (tileOffsety >= Global.Instance().chunkSize) {
							tileOffsety %= Global.Instance().chunkSize;
							chunkOffsety++;
						}
						
						MapChunk c = Global.Instance().chunks.get(new Vector2(chunkOffsetx, chunkOffsety));
						c.tileMap.getTile(tileOffsetx, tileOffsety).index = tileIndex[i][j];
						
						tileOffsety++;
					}
				} catch (Exception e) {}
				
				tileOffsetx++;
			}
		} catch (Exception e) {}
	}
}
